use actix_web::web::{Data, Json};

use crate::{
    common::{app::tags::AUTH, response::ApiResponse, types::ApiResult},
    config::{auth::AuthInfo, state::AppState},
    models::{
        request::auth_req::{DisableRequest, LoginRequest},
        response::auth_res::LoginResponse,
    },
    services::auth_service as AuthService,
};

/// 登录
#[utoipa::path(
    tags = [AUTH],
    post,
    path = "/api/v1/auth/login",
    responses(
        (status = 200, description = "Ok", body=LoginResponse),
        (status = 400, description = "Bad Request"),
        (status = 401, description = "Unauthorized"),
        (status = 500, description = "Internal Server Error")
    ),
    request_body = LoginRequest
)]
pub async fn login(
    state: Data<AppState>,
    login_req: Json<LoginRequest>,
) -> ApiResult<LoginResponse> {
    AuthService::login(state, login_req.into_inner()).await
}

/// 登出
#[utoipa::path(
    tags = [AUTH],
    post,
    path = "/api/v1/auth/logout",
    responses(
        (status = 200, description = "Ok", body=bool),
        (status = 400, description = "Bad Request"),
        (status = 401, description = "Unauthorized"),
        (status = 500, description = "Internal Server Error")
    )
)]
pub async fn logout(state: Data<AppState>, auth_info: AuthInfo) -> ApiResult<bool> {
    AuthService::logout(state, auth_info).await
}

/// 禁用用户
#[utoipa::path(
    tags = [AUTH],
    post,
    path = "/api/v1/auth/disable",
    responses(
        (status = 200, description = "Ok", body=bool),
        (status = 400, description = "Bad Request"),
        (status = 401, description = "Unauthorized"),
        (status = 500, description = "Internal Server Error")
    ),
    request_body = DisableRequest
)]
pub async fn disable(
    state: Data<AppState>,
    disable_req: Json<DisableRequest>,
    auth_info: AuthInfo,
) -> ApiResult<bool> {
    AuthService::disable(state, disable_req.into_inner(), auth_info).await
}

/// 启用用户
#[utoipa::path(
    tags = [AUTH],
    post,
    path = "/api/v1/auth/enable",
    responses(
        (status = 200, description = "Ok", body=ApiResponse<bool>),
        (status = 400, description = "Bad Request"),
        (status = 401, description = "Unauthorized"),
        (status = 500, description = "Internal Server Error")
    ),
    request_body = DisableRequest
)]
pub async fn enable(
    state: Data<AppState>,
    disable_req: Json<DisableRequest>,
    auth_info: AuthInfo,
) -> ApiResult<bool> {
    AuthService::enable(state, disable_req.into_inner(), auth_info).await
}
